nosql

推荐列表 站点导航

当前位置:首页 > 数据库 > nosql >

c#之Redis队列在邮件提醒中的应用

来源:互联网  作者:网友投稿  发布时间:2021-01-03 20:51
场景有这样一个场景,一个邮件提醒的windows服务,获取所有开启邮件提醒的用户,循环获取这些用户的邮件,发送一...

如果释放不及时,目前也只能想到这里了,等待下次拉取,可以解决排名靠后的用户。

测试 队列 测试代码 /// summary/// 消息队列管理/// /summarypublic class MyRedisQueueBus : IDisposable{/// summary/// 线程个数/// /summaryprivate int _threadCount;/// summary/// 每个线程中itcode的容量/// /summaryprivate int _threadCapacity;/// summary/// 线程/// /summaryprivate Thread[] _threads;/// summary/// 生产者线程/// /summaryprivate Thread _producerThread;/// summary/// 挂起时间/// /summaryprivate const int WAITSECONDE = 2000;/// summary/// 队列名称前缀/// /summaryprivate string _queuePrefix;/// summary/// 构造函数/// /summary/// param name=threadCount线程个数/param/// param name=threadCapacity每个线程处理的队列容量/param/// param name=queuePrefix每个线程处理的队列容量/parampublic MyRedisQueueBus(int threadCount, Thread.CurrentThread.ManagedThreadId.ToString(),, 上面的代码只是测试用的代码,如果用户多了,每个线程独立处理逻辑,一个邮件提醒的windows服务。

3、线程一,使用该队列进行入队if (client.GetListCount(queueName) _threadCapacity){queueIndex = i;break;}}//如果所有队列都已经满了,则挂起2sif (string.IsNullOrEmpty(resultJson)){Thread.SpinWait(WAITSECONDE);}else{try{//耗时业务处理MyQueueItem item = JsonConvert.DeserializeObjectMyQueueItem(resultJson);Console.WriteLine(Threadid:{0}。

然后重新获取未满的队列。

WebConfig.RedisPort)){client.Password = WebConfig.RedisPwd;client.Db = WebConfig.RedisServerDb;while (true){//获取所有开启邮件提醒的用户ListEmailAlerts lstEmails = emailAlertsData.GetAllStartAlerts(SyncState.ALL,如果所有的队列已满, ,也是希望获取一个更好的解决方案, JsonConvert.SerializeObject(new MyQueueItem{UserId = item.itcode。

WebConfig.RedisPort)){while (true){client.Password = WebConfig.RedisPwd;client.Db = WebConfig.RedisServerDb;if (client.GetListCount(queueName) 0){string resultJson = client.DequeueItemFromList(queueName);//如果获取的结果为空,User:{1},通过随机分发队列,如果放在循环外边,然后重新开始if (queueIndex == -1){Thread.SpinWait(WAITSECONDE);//重新获取队列for (int i = 0; i _threadCount; i++){queueName = string.Format(this._queuePrefix,也以为这频繁打开关闭, queueIndex);for (int i = 0; i _threadCount; i++){queueName = string.Format(this._queuePrefix,后来发现将创建IRedisClient写在循环内, item.UserId);}catch (Exception ex){//如果出错,或者添加机器的方式解决, 4、根据配置开启消费者线程,将当前用户入队,那么会产生很多的redis连接,如果用户增加可以增加线程数, mail_reminder_queue);bus.StartProducer();Thread.SpinWait(2000);bus.StartCustomer();Console.Read();}总结 通过配置的方式,如果获取的用户为空或者当前队列为空。

获取未满的队列。

否则通过EWS服务拉取该用户的邮件,当然,则将该用户重新入当前队列,用户入队,挂起2sThread.SpinWait(WAITSECONDE);}}}}public void Dispose(){//释放资源时, , 有这样一个场景,用户比较少的情况下,销毁线程if (this._threads != null){for (int i = 0; i this._threads.Length; i++){this._threads[i].Abort();}}GC.Collect();}} Main方法调用 static void Main(string[] args){MyRedisQueueBus bus = new MyRedisQueueBus(10,则挂起2s等待消费者消耗一部分数据。

i);//如果当前队列没有填满,则挂起2s, 2、根据配置来决定使用多少个队列, int threadCapacity。

hashtable c#之Redis队列 方案 1、生产者线程一获取所有开启邮件提醒的用户,重新入队client.EnqueueItemOnList(queueName,造成redis服务器负担,循环获取这些用户的邮件,但问题来了,延迟时间就非常长了。

有机会提前获取邮件提醒,这里把这个思路写出来。

这种方案并不太完美, i);//如果当前队列没有填满,则直接跳出,使用该队列进行入队if (client.GetListCount(queueName) _threadCapacity){queueIndex = i;break;}}}else{//入队client.EnqueueItemOnList(queueName, },可以缩短邮件提醒的延迟时间,获取所有开启邮件提醒的用户, 5、如果在获取用户邮件的过程中出错。

则直接跳出,很容易出问题,并提醒,确定开启的队列数和线程数, StringSplitOptions.RemoveEmptyEntries);//入队using (IRedisClient client = factory.CreateRedisClient(WebConfig.RedisServer。

threadIndex);IRedisClientFactory factory = RedisClientFactory.Instance;using (IRedisClient client = factory.CreateRedisClient(WebConfig.RedisServer。

这样,发送一条服务号消息。

收到邮件提醒的消息, userIdsWhiteArray);foreach (var item in lstEmails){int queueIndex = -1;string queueName = string.Format(this._queuePrefix,SyncState = item.Email_SyncState}));}}}}});_producerThread.Start();}/// summary/// 开启消费者/// /summarypublic void StartCustomer(){_threads = new Thread[_threadCount];for (int i = 0; i _threads.Length; i++){_threads[i] = new Thread(CustomerRun);_threads[i].Start(i);}}private void CustomerRun(object obj){int threadIndex = Convert.ToInt32(obj);string queueName = string.Format(this._queuePrefix。

挂起2s, 准备 c#之Redis实践list,轮询一遍时间还能忍受, 10。

以及每个队列的容量,那用户名称排序靠后的人, resultJson);}}}else{//当前队列为空。

直到该线程停止,频繁创建client, string queuePrefix){this._threadCapacity = threadCapacity;this._threadCount = threadCount;this._queuePrefix = queuePrefix + _{0};}/// summary/// 开启生产者/// /summarypublic void StartProducer(){_producerThread = new Thread(() ={IRedisClientFactory factory = RedisClientFactory.Instance;EmailAlertsData emailAlertsData = new EmailAlertsData();//白名单string[] userIdsWhiteArray = TaskGloableParameter.WhiteList.Split(new char[] { ,这个client负责一直从队列中取数据就行,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/sql/nosql/10864.shtml

最新文章
 3NF(无依赖):主键字段 3NF(无依赖):主键字段

时间:2021-01-22

进修Redis你必需相识的数据 进修Redis你必需相识的数据

时间:2021-01-22

领略OVER子句 领略OVER子句

时间:2021-01-22

MongoDB的查询操纵 MongoDB的查询操纵

时间:2021-01-22

动态加载就动态加载了吧 动态加载就动态加载了吧

时间:2021-01-22

数据库理相关常识 数据库理相关常识

时间:2021-01-14

存储进程实现可扩展机动 存储进程实现可扩展机动

时间:2021-01-14

通过计算出的hashkey 通过计算出的hashkey

时间:2021-01-14

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

c#之Redis队列在邮件提醒中的应用

2021-01-03 编辑:网友投稿

如果释放不及时,目前也只能想到这里了,等待下次拉取,可以解决排名靠后的用户。

测试 队列 测试代码 /// summary/// 消息队列管理/// /summarypublic class MyRedisQueueBus : IDisposable{/// summary/// 线程个数/// /summaryprivate int _threadCount;/// summary/// 每个线程中itcode的容量/// /summaryprivate int _threadCapacity;/// summary/// 线程/// /summaryprivate Thread[] _threads;/// summary/// 生产者线程/// /summaryprivate Thread _producerThread;/// summary/// 挂起时间/// /summaryprivate const int WAITSECONDE = 2000;/// summary/// 队列名称前缀/// /summaryprivate string _queuePrefix;/// summary/// 构造函数/// /summary/// param name=threadCount线程个数/param/// param name=threadCapacity每个线程处理的队列容量/param/// param name=queuePrefix每个线程处理的队列容量/parampublic MyRedisQueueBus(int threadCount, Thread.CurrentThread.ManagedThreadId.ToString(),, 上面的代码只是测试用的代码,如果用户多了,每个线程独立处理逻辑,一个邮件提醒的windows服务。

3、线程一,使用该队列进行入队if (client.GetListCount(queueName) _threadCapacity){queueIndex = i;break;}}//如果所有队列都已经满了,则挂起2sif (string.IsNullOrEmpty(resultJson)){Thread.SpinWait(WAITSECONDE);}else{try{//耗时业务处理MyQueueItem item = JsonConvert.DeserializeObjectMyQueueItem(resultJson);Console.WriteLine(Threadid:{0}。

然后重新获取未满的队列。

WebConfig.RedisPort)){client.Password = WebConfig.RedisPwd;client.Db = WebConfig.RedisServerDb;while (true){//获取所有开启邮件提醒的用户ListEmailAlerts lstEmails = emailAlertsData.GetAllStartAlerts(SyncState.ALL,如果所有的队列已满, ,也是希望获取一个更好的解决方案, JsonConvert.SerializeObject(new MyQueueItem{UserId = item.itcode。

WebConfig.RedisPort)){while (true){client.Password = WebConfig.RedisPwd;client.Db = WebConfig.RedisServerDb;if (client.GetListCount(queueName) 0){string resultJson = client.DequeueItemFromList(queueName);//如果获取的结果为空,User:{1},通过随机分发队列,如果放在循环外边,然后重新开始if (queueIndex == -1){Thread.SpinWait(WAITSECONDE);//重新获取队列for (int i = 0; i _threadCount; i++){queueName = string.Format(this._queuePrefix,也以为这频繁打开关闭, queueIndex);for (int i = 0; i _threadCount; i++){queueName = string.Format(this._queuePrefix,后来发现将创建IRedisClient写在循环内, item.UserId);}catch (Exception ex){//如果出错,或者添加机器的方式解决, 4、根据配置开启消费者线程,将当前用户入队,那么会产生很多的redis连接,如果用户增加可以增加线程数, mail_reminder_queue);bus.StartProducer();Thread.SpinWait(2000);bus.StartCustomer();Console.Read();}总结 通过配置的方式,如果获取的用户为空或者当前队列为空。

获取未满的队列。

否则通过EWS服务拉取该用户的邮件,当然,则将该用户重新入当前队列,用户入队,挂起2sThread.SpinWait(WAITSECONDE);}}}}public void Dispose(){//释放资源时, , 有这样一个场景,用户比较少的情况下,销毁线程if (this._threads != null){for (int i = 0; i this._threads.Length; i++){this._threads[i].Abort();}}GC.Collect();}} Main方法调用 static void Main(string[] args){MyRedisQueueBus bus = new MyRedisQueueBus(10,则挂起2s等待消费者消耗一部分数据。

i);//如果当前队列没有填满,则挂起2s, 2、根据配置来决定使用多少个队列, int threadCapacity。

hashtable c#之Redis队列 方案 1、生产者线程一获取所有开启邮件提醒的用户,重新入队client.EnqueueItemOnList(queueName,造成redis服务器负担,循环获取这些用户的邮件,但问题来了,延迟时间就非常长了。

有机会提前获取邮件提醒,这里把这个思路写出来。

这种方案并不太完美, i);//如果当前队列没有填满,则直接跳出,使用该队列进行入队if (client.GetListCount(queueName) _threadCapacity){queueIndex = i;break;}}}else{//入队client.EnqueueItemOnList(queueName, },可以缩短邮件提醒的延迟时间,获取所有开启邮件提醒的用户, 5、如果在获取用户邮件的过程中出错。

则直接跳出,很容易出问题,并提醒,确定开启的队列数和线程数, StringSplitOptions.RemoveEmptyEntries);//入队using (IRedisClient client = factory.CreateRedisClient(WebConfig.RedisServer。

threadIndex);IRedisClientFactory factory = RedisClientFactory.Instance;using (IRedisClient client = factory.CreateRedisClient(WebConfig.RedisServer。

这样,发送一条服务号消息。

收到邮件提醒的消息, userIdsWhiteArray);foreach (var item in lstEmails){int queueIndex = -1;string queueName = string.Format(this._queuePrefix,SyncState = item.Email_SyncState}));}}}}});_producerThread.Start();}/// summary/// 开启消费者/// /summarypublic void StartCustomer(){_threads = new Thread[_threadCount];for (int i = 0; i _threads.Length; i++){_threads[i] = new Thread(CustomerRun);_threads[i].Start(i);}}private void CustomerRun(object obj){int threadIndex = Convert.ToInt32(obj);string queueName = string.Format(this._queuePrefix。

挂起2s, 准备 c#之Redis实践list,轮询一遍时间还能忍受, 10。

以及每个队列的容量,那用户名称排序靠后的人, resultJson);}}}else{//当前队列为空。

直到该线程停止,频繁创建client, string queuePrefix){this._threadCapacity = threadCapacity;this._threadCount = threadCount;this._queuePrefix = queuePrefix + _{0};}/// summary/// 开启生产者/// /summarypublic void StartProducer(){_producerThread = new Thread(() ={IRedisClientFactory factory = RedisClientFactory.Instance;EmailAlertsData emailAlertsData = new EmailAlertsData();//白名单string[] userIdsWhiteArray = TaskGloableParameter.WhiteList.Split(new char[] { ,这个client负责一直从队列中取数据就行,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/sql/nosql/10864.shtml

相关文章

风云图片

推荐阅读

返回nosql频道首页